package com.laikan.reader.bookreader.engines;

import android.text.TextUtils;

/**
 * Created by wx
 *
 * @date 2016/5/26/10:21
 * @description 命创建类
 */
public class CmdBuild extends CmdBase {

    public CmdBuild(String text) {
        super(text);
    }

    /**
     * 创建命令行
     *
     * @param chapterName
     * @param content
     * @param zzs
     *
     * @return
     */
    public static String createNormalCmd(String chapterName, String content, String zzs) {
        // 获取到章节内容
        String name = chapterName.replace("  ", " ");
        name = name.replace("  ", " ");
        String s = content.replace("<br/>", "\n");

        StringBuilder sb = new StringBuilder();
        sb.append("┣╋font┃title┃size=25┃color=0xFF000000┃").append(name).append("╋┫");
        sb.append("\n┣╋line┃lineWidth=2┃lineColor=0xffffff╋┫");
        sb.append("\n" + s);
        if(!TextUtils.isEmpty(zzs)) {// 作者说第一个段落用
            boolean isFristP = true;// 是否作者说的 第一个段落
            zzs = zzs.replace(" ", "");
            zzs += "\n";

            char[] arr = zzs.toCharArray();
            int pos = 0;
            StringBuilder paragraph = new StringBuilder(200);
            for(char ch : arr) {
                if(ch != '\n') {
                    paragraph.append(ch);
                    continue;
                }
                String p = paragraph.toString();
                paragraph.setLength(0);
                pos += p.toCharArray().length;
                p = p.trim();
                if(p.equals("")) {
                    continue;
                }
                if(isFristP) {
                    isFristP = false;
                    sb.append("\n┣╋font┃zzs┃size=20┃color=0xFF00ff00┃").append(p).append("╋┫");
                }else {
                    sb.append("\n┣╋font┃zzs┃size=20┃color=0xFF00ff00┃").append(p).append("╋┫");
                }
            }
        }

        sb.append("\n" + "┣╋" + "interaction" + "╋┫" + "\n");
        return sb.toString();

    }


    public static String GetValue(String string) {
        int i = string.indexOf('=');
        if(i == -1) {
        }
        return string.substring(i + 1);
    }

    /**
     * 获取段落内容
     *
     * @param p
     *
     * @return
     */
    public static char[] getContent(String p) {
        int start = p.lastIndexOf(STag);
        if(start == -1) {
            return p.toCharArray();
        }
        int end = p.lastIndexOf(ETag);
        if(end == -1) {
            return p.toCharArray();
        }
        return p.substring(start + 1, end).toCharArray();
    }

    public static String getCommand(String string, String p) {

        int end = p.lastIndexOf(STag);
        if(end == -1) {
            return string;
        }
        return p.substring(0, end + 1) + string + ETag;
    }

}
